package com.jhws.sys.cms.controller;

import com.jhws.sys.cms.bo.CommGroup;
import com.jhws.sys.user.bo.TblUserDimBo;
import com.jhws.common.base.bo.JsonRespData;
import com.jhws.sys.user.service.ITblUserDimService;
import com.jhws.sys.cms.service.CommGroupService;
import com.jhws.sys.cms.service.ResourceService;
import com.jhws.util.StringUtils;
import org.apache.commons.collections.MapUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/CommGroupCtrl")
public class CommGroupCtrl {
    @Resource
    private ITblUserDimService IUserService;
    @Resource
    private CommGroupService commGroupService;
    @Resource
    private ResourceService resourceService;

    @RequestMapping("saveCommGroup.x")
    @ResponseBody
    public JsonRespData saveCommGroup(CommGroup commGroup, String commCodes) throws Exception {
        TblUserDimBo userDimBo = IUserService.currentUser();
        commGroup.setCreator(userDimBo.getLogName());
        commGroup.setParentGroupId("-1");
        if (StringUtils.isNotEmpty(commGroup.getGroupId())) {
            commGroupService.deleteCommGroupByWhere("where parent_group_id='" + commGroup.getGroupId() + "'");
        }
        commGroupService.saveCommGroup(commGroup);
        commGroup.setParentGroupId(commGroup.getGroupId());
        String[] commCodeArray = commCodes.split(",");

       /* StringBuilder commCodeIn = new StringBuilder();
        if (commCodeArray.length > 0) {
            for (String commCode : commCodeArray) {
                if (StringUtils.isEmpty(commCodeIn.toString())) {
                    commCodeIn = new StringBuilder(" and comm_code in('" + commCode + "'");
                } else {
                    commCodeIn.append(",'").append(commCode).append("'");
                }
            }
            commCodeIn.append(")");
            commGroupService.deleteCommGroupByWhere("where 1=1 " + commCodeIn);
        }*/

        for (String commCode : commCodeArray) {
            commGroup.setGroupId("");
            commGroup.setCommCode(commCode);
            commGroupService.saveCommGroup(commGroup);
        }
        return new JsonRespData("", 1, "");
    }

    @RequestMapping("deleteCommGroup.x")
    @ResponseBody
    public JsonRespData deleteCommGroup(String groupId) throws Exception {
        commGroupService.deleteCommGroupByWhere("where parent_group_id='" + groupId + "' or id='" + groupId + "'");
        //删除小区分组需要删除组内所有的cms资源
        resourceService.deleteResource(" where commGroupId='" + groupId + "'");
        return new JsonRespData("", 1, "");
    }

    /**
     * 判断小区分组是否存在
     */
    @RequestMapping("isGroup.x")
    @ResponseBody
    public JsonRespData isGroup(@RequestParam Map param) {
        String commCode = MapUtils.getString(param, "commCode", "");
        String groupId = MapUtils.getString(param, "groupId", "");
        String where = " where 1=1 ";
        if (StringUtils.isNotEmpty(commCode)) {
            where += "and comm_code='" + commCode + "' ";
        } else {
            return new JsonRespData("", -1, "小区编号不能为空！");
        }

        if (StringUtils.isNotEmpty(groupId)) {
            where += "and parent_group_id !='" + groupId + "' ";
        }
        List<CommGroup> commGroups = commGroupService.findCommGroupListByWhere(where);
        Map<String, Object> ret = new HashMap<>();
        if (commGroups.size() > 0) {
            ret.put("isGroup", true);
            ret.put("commGroupList", commGroups);
            return new JsonRespData("", 1, ret);
        }
        ret.put("isGroup", false);
        return new JsonRespData("", 1, ret);
    }

    /**
     * 查询小区分组
     */
    @SuppressWarnings("unchecked")
    @RequestMapping("queryCommGroup.x")
    @ResponseBody
    public Object queryCommGroup(@RequestParam Map param) throws Exception {
        param.put("parentGroupId", "-1");
        return commGroupService.findCommGroupList(param);
    }

    /**
     * 查询小区分组信息
     */
    @RequestMapping("queryCommGroupInfo.x")
    @ResponseBody
    public Object queryCommGroupInfo(@RequestParam Map param) throws Exception {
        return commGroupService.findCommGroupList(param);
    }
}
